package com.zhupanlin.zhyl.user.api.controller;

import com.zhupanlin.zhyl.common.result.Result;
import com.zhupanlin.zhyl.common.utils.AuthContextHolder;
import com.zhupanlin.zhyl.model.user.Patient;
import com.zhupanlin.zhyl.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/12/6 23:47
 */
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {

    @Autowired
    private PatientService patientService;

    // 获取就诊人列表
    @GetMapping("auth/findAll")
    public Result findAll(HttpServletRequest request) {
        // 获取当前登录用户的id
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> list = patientService.findAllUserId(userId);
        return Result.ok(list);
    }

    // 添加就诊人
    @PostMapping("auth/save")
    public Result savePatient(@RequestBody Patient patient, HttpServletRequest request) {

        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();

    }

    // 根据 id 获取就诊人信息
    @GetMapping("auth/get/{id}")
    public Result getPatient(@PathVariable("id") Long id) {
        Patient patient = patientService.getPatientById(id);
        return Result.ok(patient);

    }

    // 修改就诊人
    @PostMapping("auth/update")
    public Result updatePatient(@RequestBody Patient patient) {
        patientService.updateById(patient);
        return Result.ok();
    }

    // 删除就诊人
    @DeleteMapping("auth/remove/{id}")
    public Result removePatient(@PathVariable("id") Long id) {
        patientService.removeById(id);
        return Result.ok();
    }

    // 根据就诊人id获取就诊人信息
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(@PathVariable("id") Long id) {
        return patientService.getPatientById(id);
    }


}
